package com.bjpowernode.service.impl;

import com.bjpowernode.constant.Constants;
import com.bjpowernode.manager.CustomerManager;
import com.bjpowernode.mapper.TCustomerMapper;
import com.bjpowernode.model.TCustomer;
import com.bjpowernode.query.CustomerQuery;
import com.bjpowernode.result.CustomerExcel;
import com.bjpowernode.service.CustomerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Resource
    private CustomerManager customerManager;

    @Resource
    private TCustomerMapper tCustomerMapper;

    @Override
    public Boolean convertCustomer(CustomerQuery customerQuery) {
        return customerManager.convertCustomer(customerQuery);
    }

    @Override
    public PageInfo<TCustomer> getCustomerByPage(Integer current) {
        PageHelper.startPage(current, Constants.PAGE_SIZE);
        List<TCustomer> list = tCustomerMapper.selectCustomerPage();
        PageInfo<TCustomer> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public List<CustomerExcel> getCustomerByExcel(String ids) {
        List<CustomerExcel> customerExcelList = new ArrayList<>();

        List<String> idsList = StringUtils.hasText(ids) ? Arrays.asList(ids.split(",")): Collections.EMPTY_LIST;

        List<TCustomer> tCustomerList = tCustomerMapper.selectCustomerByExcel(idsList);
        // 把从数据库查询出来的数据转换为List<CustomerExcel>
        tCustomerList.forEach(tCustomer -> {
            CustomerExcel customerExcel = new CustomerExcel();
            customerExcel.setOwnerName(ObjectUtils.isEmpty(tCustomer.getOwnerDo()) ? Constants.EMPTY : tCustomer.getOwnerDo().getName());
            customerExcel.setActivityName(ObjectUtils.isEmpty(tCustomer.getActivityDo()) ? Constants.EMPTY : tCustomer.getActivityDo().getName());
            customerExcel.setFullName(tCustomer.getClueDo().getFullName());
            customerExcel.setAppellationName(ObjectUtils.isEmpty(tCustomer.getAppellationDo()) ? Constants.EMPTY : tCustomer.getAppellationDo().getTypeValue());
            customerExcel.setPhone(tCustomer.getClueDo().getPhone());
            customerExcel.setWeixin(tCustomer.getClueDo().getWeixin());
            customerExcel.setQq(tCustomer.getClueDo().getQq());
            customerExcel.setEmail(tCustomer.getClueDo().getEmail());
            customerExcel.setAge(tCustomer.getClueDo().getAge());
            customerExcel.setJob(tCustomer.getClueDo().getJob());
            customerExcel.setYearIncome(tCustomer.getClueDo().getYearIncome());
            customerExcel.setAddress(tCustomer.getClueDo().getAddress());
            customerExcel.setNeedLoadName(ObjectUtils.isEmpty(tCustomer.getNeedLoanDo()) ? Constants.EMPTY : tCustomer.getNeedLoanDo().getTypeValue());
            customerExcel.setProductName(ObjectUtils.isEmpty(tCustomer.getIntentionProductDo()) ? Constants.EMPTY : tCustomer.getIntentionProductDo().getName());
            customerExcel.setSourceName(ObjectUtils.isEmpty(tCustomer.getSourceDo()) ? Constants.EMPTY : tCustomer.getSourceDo().getTypeValue());
            customerExcel.setDescription(tCustomer.getDescription());
            customerExcel.setNextContactTime(tCustomer.getNextContactTime());

            customerExcelList.add(customerExcel);
        });
        return customerExcelList;
    }
}
